<template>
  <view>

    <view class="xuanxiangka">
      <view :class="cindex == 0 ? 'active':'' " @click="getworkercontract(0)" v-if="usertype != 4">劳工合同</view>
      <view :class="cindex == 1 ? 'active': '' " @click="getmincontract(1)" style="padding-left: 40rpx;">我的合同</view>
    </view>

    <view class="null-box" v-if="contractmin.length == 0">
      <view class="null-image">
        <image src="/static/images/777.png" mode=""></image>
      </view>
      空空如也
    </view>

    <view class="contract-list" style="padding-bottom: 80rpx;" v-for="(item,index) in contractmin" :key="index"
      @click="tocontractmodel(item.id,item.contractStatus)">
      <view class="contract-title">
        <view class="contract-img">
          <view class="image">
            <image src="../../../static/images/组 336.png"></image>
          </view>
          <view class="title-text">
            {{item.projectName}}{{agreementOrContract(item.workerIdentityCard,item.workerSex)?'签约合同':'签约协议'}}
          </view>
        </view>
        <view class="contract-tright">
          <view :class="item.contractStatus==='已签约'?'point1':'point2'"></view>
          <view :class="item.contractStatus==='已签约'?'yfontcolor':'nfontcolor'">{{item.contractStatus}}</view>
          <!-- 状态要更改 -->
        </view>
      </view>
      <view class="contract-info">
        <view class="contract-itext">
          <view class="">合同编号：{{item.contractId}}</view>
          <view class="">发行日期:{{item.contractWorkStart.replace('00:00:00', '')}}</view>
        </view>
        <view class="contract-itext" style="margin-left: 10rpx;">
          <view class="">签约人：{{item.workerName}}</view>
          <view class="">
            签约日期:{{item.contractPersonTwoTime == null ? '未签约' :item.contractPersonTwoTime.replace('00:00:00', '')}}
          </view>
        </view>
      </view>
    </view>

    <view class="sendcontract" v-if="cindex == 0 && usertype != 4">
      <button @click="addContract(1)">发起简约合同</button>
      <button @click="addContract(0)">发起详细合同</button>
    </view>


  </view>
</template>
<script>
  export default {
    data() {
      return {
        usercardId: '',
        nickName: '',
        projectId: '',
        projectName: '',
        usertype: 4,
        cindex: 0,
        contractStatus: 0,
        contractWorkEnd: "",
        contractWorkStart: "",
        contractWorkTimeType: 0,
        id: 0,
        isEffective: 1,
        orderBy: "",
        pageNo: 1,
        pageSize: 100000,
        returnFields: "",
        unitName: "",
        workerName: "",
        contract: [],
        contractmin: [],
        contractStatus: []
      }
    },
    onLoad(options) {
      this.usertype = uni.getStorageSync('usertype');
      this.nickName = uni.getStorageSync('nickName')
      this.usercardId = uni.getStorageSync('userCode')
      if (this.usertype == 5) {
        this.usertype = 4

      }
    },
    created() {
      // this.getContract();
    },
    onShow() {
      this.getContract();
    },
    methods: {
	  agreementOrContract(idCode,sex){//年龄判断超出显示协议
		  if(sex==0&&this.calculateAge(idCode)>=50){
			  return false
		  }
		  if(sex==1&&this.calculateAge(idCode)>=60){
			  return false
		  }
		  return true
	  },
	  calculateAge(idNumber) {
	  		  // 从身份证号码中提取出生日期，假设身份证号码中出生日期的格式为"YYYYMMDD"
	  		   const birthYear = parseInt(idNumber.substr(6, 4), 10); const birthMonth = parseInt(idNumber.substr(10, 2), 10);
	  			const birthDay = parseInt(idNumber.substr(12, 2), 10); 
	  			// 获取当前日期 
	  			const currentDate = new Date(); 
	  			const currentYear = currentDate.getFullYear(); 
	  			const currentMonth = currentDate.getMonth() + 1; 
	  			// 月份从0开始，所以要加1
	  			 const currentDay = currentDate.getDate();
	  			  // 计算年龄 
	  			  let age = currentYear - birthYear; 
	  			  // 根据出生月份和日期判断是否已经过生日 
	  			  if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) { age--; }
	  			   return age; 
	  },
      // 0 表示已签约 1编辑中 2待签约
      tocontractmodel(id, status) { //点击合同列表执行跳转方法
        uni.navigateTo({
          url: './signingcontract?id=' + id + '&status=' + status
        })
      },
      addContract(num) {
        uni.redirectTo({
          url: './contractmodel?id=' + num
        })
      },
      changecolor(num) {
        this.index = num;
      },
      getContract() {
        this.contractmin = []
        console.log('获取合同信息了---------');
        this.cindex = 0
        uni.showLoading({
          title: '数据加载中'
        })
        uni.$post(this.path + '/contract/queryAll', {}).then(res => {
          if (res.code == 200) {
            this.contract = res.data.records
            this.contract.sort((a, b) => {
              return b.contractStatus - a.contractStatus
            }) //排序数组未签约在前，已签约在后
            // 0 表示已签约 1编辑中 2待签约
            for (let i = 0; i < this.contract.length; i++) {
              let item = this.contract[i]
              if (item.contractStatus == 0) {
                item.contractStatus = '已签约'
              } else if (item.contractStatus == 1) {
                item.contractStatus = '编辑中'
              } else {
                item.contractStatus = '待签约'
              }
              console.log('item.contractPersonTwoTime', item.contractPersonTwoTime);
              item.contractWorkAddress = item.contractWorkAddress + '签约合同'
            }
            for (let i = 0; i < this.contract.length; i++) { //劳工合同
              if (this.nickName != this.contract[i].workerName && this.usercardId != this.contract[i]
                .workerIdentityCard) {
                console.log('--------');
                this.contractmin.push(this.contract[i])
              }
            }
            uni.hideLoading()
            // 当我进入页面加载完毕之后 直接进行合同得到筛选
            if (this.usertype == 4) {
              this.getmincontract(1)
            }
          } else {
            uni.hideLoading()
            // uni.showToast({
            //   title: '数据加载失败, 请重试',
            //   icon: 'error'
            // })
          }
        })
        this.cindex = 0
      },
      getworkercontract(id) {
        //点击我的合同 然后你通过名字来过滤自己名字的合同
        this.cindex = id
        this.contractmin = []
        for (let i = 0; i < this.contract.length; i++) { //劳工合同
          if (this.nickName != this.contract[i].workerName && this.usercardId != this.contract[i].workerIdentityCard) {
            console.log('--------');
            this.contractmin.push(this.contract[i])
          }
        }

      },
      getmincontract(id) {
        //点击我的合同 然后你通过名字来过滤自己名字的合同
        this.cindex = id
        this.contractmin = []
        for (let i = 0; i < this.contract.length; i++) {
          if (this.nickName == this.contract[i].workerName) {
            this.contractmin.push(this.contract[i])
          }
        }
        this.contractmin.sort((a, b) => {
          return b.contractStatus - a.contractStatus
        })
      },
      getcontractInfo(e) {
        console.log('------搜索触发了');
        this.contractmin = []
        console.log('e', e);
        for (let i = 0; i < this.contract.length; i++) {
          if (this.contract[i].contractWorkAddress.indexOf(e.value) != -1) {
            console.log('into for');
            this.contractmin.push(this.contract[i])
          }
        }
        console.log('contractmin', this.contractmin);
      },
      getcontractInfoClear() {
        this.getcontractInfo({
          value: ''
        })
      }
    }
  }
</script>

<style lang="scss">
  .null-box {
    margin-top: 140rpx;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .point1 {
    width: 20rpx;
    height: 20rpx;
    border-radius: 50%;
    background-color: #2f67ff;
  }

  .point2 {
    width: 20rpx;
    height: 20rpx;
    border-radius: 50%;
    background-color: #ff0000;
  }


  .sendcontract {
    width: 100%;
    height: 35px;
    position: fixed;
    bottom: 10px;
    display: flex;
    justify-content: space-around;

    button {
      width: 40%;
      height: 35px;
      line-height: 35px;
      font-size: 14px;
      margin: 0 auto;
      color: white;
      background-color: #2f67ff;
    }
  }

  .xuanxiangka {
    display: flex;
    color: #b4b4b4;
    align-items: center;
    padding: 20rpx 30rpx;

    .active {
      color: black;
    }

    .xuanzhong {}

  }

  .contract-list {
    width: 95%;
    margin: 0 auto;
    margin-top: 10px;
    height: 110px;
    background-color: white;
    padding-top: 10px;
    border-radius: 8px;

    .contract-title {
      font-size: 15px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 95%;
      margin: 0 auto;

      .contract-img {
        width: calc(100% - 100px);
        height: 30px;
        display: flex;
        align-items: center;

        .image {
          width: 30px;
          height: 30px;

          image {
            width: 100%;
            height: 100%;
          }
        }

        .title-text {
          font-size: 16px;
        }
      }

      .contract-tright {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 60px;

        .yfontcolor {
          color: #2f67ff;
        }

        .nfontcolor {
          color: #ff0000;
        }
      }
    }

    .contract-info {
      margin-top: 10px;
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: center;
      align-items: center;

      .contract-itext {
        width: fit-content;
        min-width: 241rpx;
        height: 50px;
        font-size: 12px;
        color: #b4b4b4;
        display: flex;
        justify-content: space-between;
        flex-direction: column;
      }
    }
  }
</style>
